// ActionScript file
import com.helloniu.controller.UserListController;
import com.helloniu.fl.util.Tool;
import com.helloniu.model.UserListModel;
import com.helloniu.red5.info.UserInfo;

protected var oldInfo:Array = new Array();

private function initApp(e:*):void {
	UserListController.getIns().addView(this);
}

public function updateModel():void {
	var model:UserListModel = UserListController.getIns().getModel() as UserListModel;
	var newInfo:Array = model.getUsers();
	
	// users does not chnage
	if(Tool.compareUsersArray(oldInfo, newInfo)) {
		return;
	}

	oldInfo = Tool.arrayCopy(newInfo);
	
	populate();
}

private function populate():void {
	this.dataProvider = createDP();
}

protected function createDP():Array {
	var dp:Array = new Array();
	var o:Object = new Object();
	o.label = "All";
	o.data = null;
	dp.push(o);
	
	for(var i:int = 0; i < oldInfo.length; i++) {		
		var userInfo:UserInfo = oldInfo[i] as UserInfo;
		o = new Object();
		o.label = userInfo.userID;
		o.data = userInfo;
		
		dp.push(o);
	}

	return dp;	
}

public function getSelectUser():String {
	if(this.selectedItem != null) {
		return this.selectedItem.label; 
	}

	return null;
}

public function getSelectUserInfo():UserInfo {
	if(this.selectedItem != null) {
		return this.selectedItem.data as UserInfo; 
	}

	return null;
}

public function hasVideo():Boolean {
	var userInfo:UserInfo = getSelectUserInfo();

	if(userInfo != null) {
		return userInfo.binfo.videoUnit.opened; 
	}

	return false;
}